<template>
  <div class="register">
    <h2>注册</h2>
    <form @submit.prevent="handleRegister">
      <div>
        <input v-model="username" type="text" placeholder="用户名" required />
      </div>
      <div>
        <input v-model="email" type="email" placeholder="邮箱" required />
      </div>
      <div>
        <input v-model="password" type="password" placeholder="密码" required />
      </div>
      <div>
        <input v-model="confirmPassword" type="password" placeholder="确认密码" required />
      </div>
      <button type="submit">注册</button>
      <p>已有账号？<router-link to="/login">登录</router-link></p>
    </form>
  </div>
</template>

<script lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'

export default {
  name: 'Register',
  setup() {
    const router = useRouter()

    const username = ref('')
    const email = ref('')
    const password = ref('')
    const confirmPassword = ref('')

    const handleRegister = () => {
      if (password.value !== confirmPassword.value) {
        alert('密码不一致，请重新输入')
        return
      }
      console.log('用户名:', username.value)
      console.log('邮箱:', email.value)
      console.log('密码:', password.value)
      router.push('/login')  // 注册成功后跳转到登录页面
    }

    return {
      username,
      email,
      password,
      confirmPassword,
      handleRegister
    }
  }
}
</script>

<style scoped>
.register {
  max-width: 300px;
  margin: auto;
  padding: 20px;
  background-color: #f4f4f4;
  border-radius: 8px;
}

h2 {
  text-align: center;
}

input {
  width: 100%;
  padding: 10px;
  margin: 10px 0;
  border: 1px solid #ccc;
  border-radius: 4px;
}

button {
  width: 100%;
  padding: 10px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #2c9b6f;
}
</style>
